﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Domain.HU;
using HiStaff.iSystem;

namespace HiStaff.Control
{
    public partial class HiSalarySelection : HiMultiColumnComboBox
    {
        private decimal? employeeId;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public decimal? EmployeeID
        {
            get
            {
                return employeeId;
            }
            set
            {
                employeeId = value;
                ReBind();
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public HU_SALARY SalarySelected
        {
            get
            {
                if (this.SelectedItem != null) return (HU_SALARY)this.SelectedItem;
                else return null;
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public HU_SALARY GetItemByID(decimal? ID)
        {
            if (this.DataSource != null)
            {
                var obj = (from a in ((List<HU_SALARY>)this.DataSource)
                           where a.ID == ID
                           select a).ToList();
                if (obj != null && obj.Count > 0) return obj[0];
                else return null;
            }
            return null;
        }
        public string DataFieldEmployeeID { get; set; }
        public HiSalarySelection()
        {
            this.ValueMember = "ID";
            this.DisplayMember = "SALARY";
            this.ComboStyle = Janus.Windows.GridEX.ComboStyle.DropDownList;
        }
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (System.ComponentModel.LicenseManager.CurrentContext.UsageMode != System.ComponentModel.LicenseUsageMode.Designtime)
            {
                CreateCombo();
            }
        }
        public void CreateCombo()
        {
            try
            {
                Janus.Windows.GridEX.GridEXColumn col;

                //this.DropDownList.RootTable = new Janus.Windows.GridEX.GridEXTable();
                this.DropDownList.RootTable.Columns.Clear();

                col = new Janus.Windows.GridEX.GridEXColumn();
                col.Key = "SALARY";
                col.DataMember = "SALARY";
                col.Caption = "MỨC LƯƠNG";
                col.FormatString = "#,##0,##";
                col.HeaderStyle.TextAlignment = Janus.Windows.GridEX.TextAlignment.Far;
                col.ColumnType = Janus.Windows.GridEX.ColumnType.Text;
                col.Width = 80;
                this.DropDownList.RootTable.Columns.Add(col);

                col = new Janus.Windows.GridEX.GridEXColumn();
                col.Key = "SALPERCENT";
                col.DataMember = "SALPERCENT";
                col.Caption = "% MỨC HƯỞNG";
                col.FormatString = "#,##0,##";
                col.HeaderStyle.TextAlignment = Janus.Windows.GridEX.TextAlignment.Far;
                col.ColumnType = Janus.Windows.GridEX.ColumnType.Text;
                col.Width = 80;
                this.DropDownList.RootTable.Columns.Add(col);

                col = new Janus.Windows.GridEX.GridEXColumn();
                col.Key = "ALLOWANCE";
                col.DataMember = "ALLOWANCE";
                col.Caption = "PHỤ CẤP";
                col.FormatString = "#,##0,##";
                col.HeaderStyle.TextAlignment = Janus.Windows.GridEX.TextAlignment.Far;
                col.ColumnType = Janus.Windows.GridEX.ColumnType.Text;
                col.Width = 80;
                this.DropDownList.RootTable.Columns.Add(col);

                col = new Janus.Windows.GridEX.GridEXColumn();
                col.Key = "SUMSALARY";
                col.DataMember = "SUMSALARY";
                col.Caption = "TỔNG LƯƠNG";
                col.FormatString = "#,##0,##";
                col.HeaderStyle.TextAlignment = Janus.Windows.GridEX.TextAlignment.Far;
                col.ColumnType = Janus.Windows.GridEX.ColumnType.Text;
                col.Width = 80;
                this.DropDownList.RootTable.Columns.Add(col);

                col = new Janus.Windows.GridEX.GridEXColumn();
                col.Key = "CURRENCY";
                col.DataMember = "CURRENCY";
                col.Caption = "TIỀN TỆ";
                col.HeaderStyle.TextAlignment = Janus.Windows.GridEX.TextAlignment.Far;
                col.ColumnType = Janus.Windows.GridEX.ColumnType.Text;
                col.Width = 80;
                this.DropDownList.RootTable.Columns.Add(col);

                col = new Janus.Windows.GridEX.GridEXColumn();
                col.Key = "EFFECTDATE";
                col.DataMember = "EFFECTDATE";
                col.Caption = "NGÀY HIỆU LỰC";
                col.FormatString = "d";
                col.HeaderStyle.TextAlignment = Janus.Windows.GridEX.TextAlignment.Far;
                col.ColumnType = Janus.Windows.GridEX.ColumnType.Text;
                col.Width = 80;
                this.DropDownList.RootTable.Columns.Add(col);

                col = new Janus.Windows.GridEX.GridEXColumn();
                col.Key = "EXPIREDATE";
                col.DataMember = "EXPIREDATE";
                col.Caption = "NGÀY HẾT HIỆU LỰC";
                col.FormatString = "d";
                col.HeaderStyle.TextAlignment = Janus.Windows.GridEX.TextAlignment.Far;
                col.ColumnType = Janus.Windows.GridEX.ColumnType.Text;
                col.Width = 80;
                this.DropDownList.RootTable.Columns.Add(col);
            }
            catch (Exception ex)
            {
                this.Text = ex.Message;
            }
        }
        public void ReBind()
        {
            try
            {
                if (employeeId.HasValue)
                {
                    this.DataSource = null;
                    var lstData = Service.ProfileClient.GetSalary(new HU_SALARY
                    {
                        EMPLOYEEID = employeeId,
                        STATUS = 1
                    });
                    lstData.Insert(0, new HU_SALARY());
                    this.DataSource = lstData;
                    this.Refresh();
                    this.SelectedIndex = 0;
                }
                else
                {
                    this.DataSource = null;
                    this.Refresh();
                    this.SelectedIndex = -1;
                }
            }
            catch (Exception ex)
            {
                this.Text = ex.Message;
            }
        }
    }
}
